/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Dao;

import HibernateUtil.HibernateUtil;
import Interface.InterfaceExperiencia;
import Pohos.Experiencia;
import Pohos.Referencias;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Duvan11
 */
public class DaoExperiencia implements InterfaceExperiencia{
    private Session session;

    @Override
    public boolean register(Experiencia experiencia) throws Exception {
        session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        session.save(experiencia);
        transaction.commit();
        session.close();
        return true;
    }

    @Override
    public List<Experiencia> ver() throws Exception {
        session = null;
        List<Experiencia> lista = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("from Experiencia");
            lista = (List<Experiencia>) query.list();
        }catch(HibernateException e){
            System.out.println(e.getMessage());
        }
        finally{
            if(session !=null){
               session.close();
            }        
        }        
        return lista;  
    }

    @Override
    public void update(Experiencia experiencia)  {
        session = null;       
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(experiencia);
            session.getTransaction().commit();
        }catch(HibernateException e){
            System.out.println(e.getMessage());
            session.getTransaction().rollback();
        }
        finally{
            if(session !=null){
               session.close();
            }        
        }        
        
    }
    
}
